package com.zhdl.hardware_modules.service.impl;

import com.project.common.bean.BaseQueryCriteria;
import com.project.common.service.impl.BaseServiceImpl;
import com.project.network.entity.PortComm;
import com.project.network.entity.PortSocket;
import com.project.network.service.PortCommService;
import com.project.network.service.PortSocketService;
import com.zhdl.hardware.io.smartLink.repository.impl.IOCmdHandleDaoImpl;
import com.zhdl.hardware.motor.buke.repository.impl.BukePortCmdHandle;
import com.zhdl.hardware.motor.huichuan.repository.impl.HcPortCmdHandle;
import com.zhdl.hardware_modules.entity.DeviceInfo;
import com.zhdl.hardware_modules.entity.DeviceProcess;
import com.zhdl.hardware_modules.entity.ProcessStep;
import com.zhdl.hardware_modules.entity.ProcessSystem;
import com.zhdl.hardware_modules.repository.DeviceInfoRepository;
import com.zhdl.hardware_modules.repository.DeviceProcessRepository;
import com.zhdl.hardware_modules.repository.ProcessStepRepository;
import com.zhdl.hardware_modules.repository.ProcessSystemRepository;
import com.zhdl.hardware_modules.service.DeviceService;
import com.zhdl.hardware_modules.web.response.DeviceInfoResp;
import com.zhdl.hardware_modules.web.response.DeviceProcessResp;
import com.zhdl.hardware_modules.web.response.ProcessStepResp;
import com.zhdl.hardware_modules.web.response.ProcessSystemResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class DeviceServiceImpl extends BaseServiceImpl<String, DeviceInfo, DeviceInfoRepository> implements DeviceService {
    public static List<ProcessStepResp> processStepResp;
    public int systemId;
    /* -------------------串口/网口b表------------------ */
    @Resource
    private PortCommService portCommService;
    @Resource
    private PortSocketService portSocketService;
    /* -------------------数据库设备操作------------------ */
    @Resource
    private DeviceInfoRepository deviceInfoRepository;
    @Resource
    private ProcessStepRepository processStepRepository;
    @Resource
    private ProcessSystemRepository processSystemRepository;
    @Resource
    private DeviceProcessRepository deviceProcessRepository;
    /* -------------------串口/网口初始化------------------ */
    @Resource
    private HcPortCmdHandle hcPortCmdHandle;
    @Resource
    private BukePortCmdHandle bukePortCmdHandle;
    @Resource
    private IOCmdHandleDaoImpl ioCmdHandleDao;

    /**
     * 转换为输出
     *
     * @param deviceProcess 设备流程表
     */
    private static DeviceProcessResp getDeviceProcessResp(DeviceProcess deviceProcess) {
        DeviceProcessResp deviceProcessResp = new DeviceProcessResp();
        deviceProcessResp.setMId(deviceProcess.getMId());
        deviceProcessResp.setDeviceId(deviceProcess.getDeviceId());
        deviceProcessResp.setType(deviceProcess.getType());
        deviceProcessResp.setPrefix(deviceProcess.getPrefix());
        deviceProcessResp.setPort(deviceProcess.getPort());
        deviceProcessResp.setSlaveId(deviceProcess.getSlaveId());
        deviceProcessResp.setBaudRate(deviceProcess.getBaudRate());
        return deviceProcessResp;
    }

    /**
     * 硬件调试-初始化所有硬件信息
     */
    @Override
    public void hardwareInit() {
        List<DeviceInfoResp> allDevice = findAllDevice();
        for (DeviceInfoResp deviceInfoResp : allDevice) {
            if (deviceInfoResp.getTypeId() == 0) {
                hcPortCmdHandle.addPort(deviceInfoResp.getDeviceProcess().getPort());
            } else if (deviceInfoResp.getTypeId() == 1) {
                bukePortCmdHandle.addPort(deviceInfoResp.getDeviceProcess().getPort());
            } else if (deviceInfoResp.getTypeId() == 2) {
                ioCmdHandleDao.init(deviceInfoResp.getDeviceProcess().getPrefix()
                        + ":" + deviceInfoResp.getDeviceProcess().getPort());
            }
        }
        ProcessSystem processSystem;
        List<ProcessSystem> all = processSystemRepository.findAll();
        if (all.size() == 1) {
            processSystem = all.get(0);
        } else {
            processSystem = all.get(2);
        }

        /* !!!!!!!!!!特别要注意的初始化流程!!!!!!!!!!!!!! */
        processInit(processSystem.getProcessId());
//        processInit(4);
        systemId = processSystem.getProcessId();
//        systemId = 4;
    }

    /**
     * 硬件调试-初始化流程
     */
    public void processInit(int id) {
        /* 1 压滤 2 东区 3 西区 4 除渣 */
        if (id == 1) {

        } else if (id == 2) {

        } else if (id == 3) {

        } else if (id == 4) {

        }
    }

    /**
     * 硬件调试-配置设备信息
     */
    @Override
    public void setDeviceInfo() {
        BaseQueryCriteria bqc = new BaseQueryCriteria();
        List<PortComm> listCom = portCommService.findList(bqc);
        List<PortSocket> listSocket = portSocketService.findList(bqc);

        for (PortComm portComm : listCom) {
            DeviceProcess deviceProcess = new DeviceProcess();
            deviceProcess.setType("rtu");
            deviceProcess.setPrefix("com");
            deviceProcess.setPort(portComm.getPort());
            deviceProcess.setBaudRate(portComm.getBaudRate());
            deviceProcessRepository.save(deviceProcess);
        }
        for (PortSocket portSocket : listSocket) {
            DeviceProcess deviceProcess = new DeviceProcess();
            deviceProcess.setType("tcp");
            deviceProcess.setPrefix(portSocket.getServerIp());
            deviceProcess.setPort(portSocket.getServerPort());
            deviceProcessRepository.save(deviceProcess);
        }
    }

    /**
     * 硬件调试-查询当前流程下所有步骤
     *
     * @param id 车间Id
     * @return 所有步数
     */
    @Override
    public ProcessSystemResp findAllSystemStep(int id) {

        ProcessSystem processSystem = processSystemRepository.findSystemByProcessId(id);
        List<ProcessStep> processSteps = processStepRepository.findStepByProcessId(id);

        ProcessSystemResp processSystemResp = new ProcessSystemResp();
        List<ProcessStepResp> processStepResps = new ArrayList<>();

        processSystemResp.setStepCount(processSteps.size());
        processSystemResp.setSystemRemark(processSystem.getProcessRemark());
        processSystemResp.setProcessMode(processSystemResp.getProcessMode());

        for (ProcessStep processStep : processSteps) {
            ProcessStepResp processStepResp = new ProcessStepResp();
            processStepResp.setStepId(processStep.getStepId());
            processStepResp.setStepNum(processStep.getStepNum());
            processStepResp.setStepParam(processStep.getStepParam());
            processStepResp.setStepRemark(processStep.getRemark());
            processStepResp.setActionType(processStep.getActionType());
            processStepResps.add(processStepResp);
        }

        processSystemResp.setStepRespList(processStepResps);

        return processSystemResp;
    }

    /**
     * 硬件调试-修改参数
     */
    @Override
    public void setStepParam(int id, String param) {
        Optional<ProcessStep> byId = processStepRepository.findById(id);
        ProcessStep processStep = byId.get();
        processStep.setStepParam(param);
        processStepRepository.save(processStep);
    }

    /**
     * 硬件调试-查询设备详情
     *
     * @return 设备参数详情
     */
    @Override
    public List<DeviceInfoResp> findAllDevice() {
        List<DeviceProcess> deviceProcesses = deviceProcessRepository.findAll();
        List<DeviceInfo> deviceInfos = deviceInfoRepository.findDeviceInfoById();
        List<DeviceInfoResp> deviceInfoResps = new ArrayList<>();

        for (DeviceProcess deviceProcess : deviceProcesses) {
            DeviceProcessResp deviceProcessResp = getDeviceProcessResp(deviceProcess);
            DeviceInfoResp deviceInfoResp = new DeviceInfoResp();
            deviceInfoResp.setDeviceProcess(deviceProcessResp);
            for (DeviceInfo deviceInfo : deviceInfos) {
                if (deviceProcess.getDeviceId() == deviceInfo.getDeviceId()) {
                    deviceInfoResp.setId(deviceInfo.getDeviceId());
                    deviceInfoResp.setName(deviceInfo.getDeviceName());
                    deviceInfoResp.setTypeId(deviceInfo.getTypeId());
                    deviceInfoResps.add(deviceInfoResp);
                }
            }
        }
        return deviceInfoResps;
    }

    /**
     * 修改流程中的测试模式
     */
    @Override
    public void setProcessMode(int processMode) {
        if (systemId == 3) {
            Optional<ProcessSystem> byId = processSystemRepository.findById(systemId);
            ProcessSystem processSystem = byId.get();
            processSystem.setProcessMode(processMode);
            processSystemRepository.save(processSystem);
        }
    }

}
